package cn.com.se.sharepictrue.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.bean.Comment;
import cn.com.se.sharepictrue.business.CallBack;
import cn.com.se.sharepictrue.business.NetworkResponse;
import cn.com.se.sharepictrue.business.ShareApi;
import cn.com.se.sharepictrue.utils.StringUtils;
import cn.com.se.sharepictrue.utils.ToastUtils;

/**
 * 发送信息界面
 * Created by KIDNG on 2015/11/3.
 */
public class SendMessageActivity extends ActionBarActivity {
    public static final String BUNDLE_KEY_BEAN = "BUNDLE_KEY_BEAN";
    public static final String BUNDLE_KEY_TYPE = "BUNDLE_KEY_TYPE";
    public static final int TYPE_SEND_NEW = 1;
    public static final int TYPE_SEND_REPLY = 2;
    private int mType;

    @Bind(R.id.tb_send_comment_title)
    Toolbar mTbSendCommentTitle;
    @Bind(R.id.et_send_comment_content)
    EditText mEtContent;
    private Comment mCommentBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_comment);
        ButterKnife.bind(this);
        initToolbar();
        initData();
        initView();
    }

    private void initToolbar() {
        setSupportActionBar(mTbSendCommentTitle);
        mTbSendCommentTitle.setTitleTextColor(Color.WHITE);
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mType = intent.getIntExtra(BUNDLE_KEY_TYPE, TYPE_SEND_NEW);
            mCommentBean = (Comment) intent.getSerializableExtra(BUNDLE_KEY_BEAN);
        }
    }

    private void initView() {
        if (mCommentBean != null && mType != 0) {
            if (mType == TYPE_SEND_NEW) {
                mEtContent.setHint("去写些评论吧...");
            } else {
                mEtContent.setHint("回复@ " + mCommentBean.getUserName() + " :");
            }
        }
    }

    @OnClick(R.id.tv_send_comment_send)
    public void sendComment(View v) {
        String comment = mEtContent.getText().toString();
        if (!StringUtils.isEmpty(comment)) {
            if (mType == TYPE_SEND_NEW)
                sendNewComment(comment);
            else if (mType == TYPE_SEND_REPLY)
                replyComment(comment);
        }

    }

    private void replyComment(String comment) {
        if (!StringUtils.isEmpty(mCommentBean.getShareID()) &&
                !StringUtils.isEmpty(mCommentBean.getUserID()) &&
                !StringUtils.isEmpty(mCommentBean.getCommentID())) {
            Log.d("test",mCommentBean.getShareID());
            Log.d("test",mCommentBean.getUserID());
            Log.d("test",mCommentBean.getCommentID());
            Log.d("test",comment);
            ShareApi.replyComment(this, mCommentBean.getShareID(),
                    mCommentBean.getUserID(),
                    mCommentBean.getCommentID(),
                    comment,
                    new CallBack<NetworkResponse<String>>() {
                        @Override
                        public void onFail() {
                            ToastUtils.show(SendMessageActivity.this, "网络访问失败咯");
                        }

                        @Override
                        public void onResponse(NetworkResponse<String> response) {
                            if (!response.isStatus()) {
                                ToastUtils.show(SendMessageActivity.this, response.getError());
                            } else {
                                ToastUtils.show(SendMessageActivity.this, response.getError());
                                setResult(RESULT_OK);
                                finish();
                            }
                        }
                    });

        }
    }

    private void sendNewComment(String comment) {
        if (!StringUtils.isEmpty(mCommentBean.getShareID())) {
            ShareApi.sendComment(this, mCommentBean.getShareID(), comment, new CallBack<NetworkResponse<String>>() {
                @Override
                public void onFail() {
                    ToastUtils.show(SendMessageActivity.this, "网络访问失败咯");
                }

                @Override
                public void onResponse(NetworkResponse<String> response) {
                    if (!response.isStatus()) {
                        ToastUtils.show(SendMessageActivity.this, response.getError());
                    } else {
                        ToastUtils.show(SendMessageActivity.this, response.getError());
                        setResult(RESULT_OK);
                        finish();
                    }
                }
            });
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
